home *** CD-ROM | disk | FTP | other *** search
/ Freelog 125 / Freelog_MarsAvril2015_No125.iso / Musique / Quod Libet / quodlibet-3.3.0-installer.exe / bin / quodlibet / qltk / models.pyc (.txt) < prev    next >
Python Compiled Bytecode  |  2014-12-31  |  9KB  |  296 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.7)
  3.  
  4. from gi.repository import Gtk, GObject
  5. from quodlibet.qltk import pygobject_version
  6.  
  7. def _gets_marshaled_to_pyobject(obj, _types = (long, float, int, basestring, bool, GObject.Object)):
  8.     """Python objects get automarshalled to GValues which is faster than
  9.     doing it in python but also has its own mapping, because it doesn't
  10.     know the column type of the model.
  11.  
  12.     This returns if the python objects get marshalled to PYOBJECT
  13.     by the C code.
  14.  
  15.     The GType logic can be found in 'pyg_type_from_object_strict'
  16.     in PyGObject.
  17.     """
  18.     if obj is None:
  19.         return False
  20.     return not None(obj, _types)
  21.  
  22.  
  23. class _ModelMixin(object):
  24.     ATOMIC = True
  25.     
  26.     def get_value(self, iter_, column = 0):
  27.         res = super(_ModelMixin, self).get_value(iter_, column)
  28.         if isinstance(res, GObject.Value):
  29.             res = res.get_boxed()
  30.         return res
  31.  
  32.     
  33.     def get_n_columns(self):
  34.         return 1
  35.  
  36.     
  37.     def iter_changed(self, iter_):
  38.         '''Like row_changed(), but only needs a Gtk.TreeIter'''
  39.         self.row_changed(self.get_path(iter_), iter_)
  40.  
  41.     
  42.     def path_changed(self, path):
  43.         '''Like row_changed(), but only needs a Gtk.TreePath'''
  44.         self.row_changed(path, self.get_iter(path))
  45.  
  46.     
  47.     def itervalues(self, iter_ = None):
  48.         '''Yields all values'''
  49.         iter_ = self.iter_children(iter_)
  50.         getv = self.get_value
  51.         inext = self.iter_next
  52.         while iter_:
  53.             yield getv(iter_)
  54.             iter_ = inext(iter_)
  55.  
  56.     
  57.     def iterrows(self, iter_ = None):
  58.         '''Yields (iter, value) tuples'''
  59.         iter_ = self.iter_children(iter_)
  60.         getv = self.get_value
  61.         inext = self.iter_next
  62.         while iter_:
  63.             yield (iter_, getv(iter_))
  64.             iter_ = inext(iter_)
  65.  
  66.     
  67.     def is_empty(self):
  68.         return not self.get_iter_first()
  69.  
  70.     if pygobject_version >= (3, 12):
  71.         _value = GObject.Value()
  72.         _value.init(GObject.TYPE_PYOBJECT)
  73.         
  74.         def _get_marshalable(self, obj, _value = _value):
  75.             if _gets_marshaled_to_pyobject(obj):
  76.                 return obj
  77.             None.set_boxed(obj)
  78.             return _value
  79.  
  80.         del _value
  81.     else:
  82.         
  83.         def _get_marshalable(self, obj):
  84.             if _gets_marshaled_to_pyobject(obj):
  85.                 return obj
  86.             value = None.Value()
  87.             value.init(GObject.TYPE_PYOBJECT)
  88.             value.set_boxed(obj)
  89.             return value
  90.  
  91.  
  92.  
  93. class ObjectModelFilter(_ModelMixin, Gtk.TreeModelFilter):
  94.     pass
  95.  
  96.  
  97. class ObjectModelSort(_ModelMixin, Gtk.TreeModelSort):
  98.     pass
  99.  
  100.  
  101. class ObjectTreeStore(_ModelMixin, Gtk.TreeStore):
  102.     
  103.     def __init__(self, *args):
  104.         if len(args) > 1:
  105.             raise ValueError
  106.         if args and object not in args and GObject.TYPE_PYOBJECT not in args:
  107.             raise ValueError
  108.         if not args:
  109.             args = [
  110.                 object]
  111.         super(ObjectTreeStore, self).__init__(*args)
  112.  
  113.     
  114.     def append(self, parent, row = None):
  115.         if row is not None:
  116.             value = self._get_marshalable(row[0])
  117.             return self.insert_with_values(parent, -1, [
  118.                 0], [
  119.                 value])
  120.         if not not (None.ATOMIC):
  121.             raise AssertionError
  122.         return None(ObjectTreeStore, self).append(parent)
  123.  
  124.     
  125.     def insert(self, parent, position, row = None):
  126.         if row is not None:
  127.             value = self._get_marshalable(row[0])
  128.             return self.insert_with_values(parent, position, [
  129.                 0], [
  130.                 value])
  131.         if not not (None.ATOMIC):
  132.             raise AssertionError
  133.         return None(ObjectTreeStore, self).insert(parent, position)
  134.  
  135.     
  136.     def prepend(self, parent, row = None):
  137.         return self.insert(parent, 0, row)
  138.  
  139.     
  140.     def insert_before(self, parent, sibling, row = None):
  141.         if row is not None:
  142.             value = self._get_marshalable(row[0])
  143.             if sibling is None:
  144.                 position = -1
  145.             else:
  146.                 position = self.get_path(sibling)[-1]
  147.             return self.insert_with_values(parent, position, [
  148.                 0], [
  149.                 value])
  150.         if not not (None.ATOMIC):
  151.             raise AssertionError
  152.         return None(ObjectTreeStore, self).insert_before(parent, sibling)
  153.  
  154.     
  155.     def insert_after(self, parent, sibling, row = None):
  156.         if row is not None:
  157.             value = self._get_marshalable(row[0])
  158.             if sibling is None:
  159.                 position = 0
  160.             else:
  161.                 position = self.get_path(sibling)[-1] + 1
  162.             return self.insert_with_values(parent, position, [
  163.                 0], [
  164.                 value])
  165.         if not not (None.ATOMIC):
  166.             raise AssertionError
  167.         return None(ObjectTreeStore, self).insert_after(parent, sibling)
  168.  
  169.  
  170.  
  171. class ObjectStore(_ModelMixin, Gtk.ListStore):
  172.     '''Like a ListStore but only supports single column object lists
  173.  
  174.     Performance related API additions:
  175.      - append_many(), insert_many()
  176.      - itervalues()
  177.     '''
  178.     
  179.     def __init__(self, *args):
  180.         if len(args) > 1:
  181.             raise ValueError
  182.         if args and object not in args and GObject.TYPE_PYOBJECT not in args:
  183.             raise ValueError
  184.         if not args:
  185.             args = [
  186.                 object]
  187.         super(ObjectStore, self).__init__(*args)
  188.  
  189.     
  190.     def append(self, row = None):
  191.         if row:
  192.             value = self._get_marshalable(row[0])
  193.             return self.insert_with_valuesv(-1, [
  194.                 0], [
  195.                 value])
  196.         if not not (None.ATOMIC):
  197.             raise AssertionError
  198.         return None(ObjectStore, self).append(row)
  199.  
  200.     
  201.     def insert(self, position, row = None):
  202.         if row:
  203.             value = self._get_marshalable(row[0])
  204.             return self.insert_with_valuesv(position, [
  205.                 0], [
  206.                 value])
  207.         if not not (None.ATOMIC):
  208.             raise AssertionError
  209.         return None(ObjectStore, self).insert(position)
  210.  
  211.     
  212.     def iter_append_many(self, objects):
  213.         '''Append a list of python objects, yield iters'''
  214.         insert_with_valuesv = self.insert_with_valuesv
  215.         get_marshalable = self._get_marshalable
  216.         columns = [
  217.             0]
  218.         
  219.         try:
  220.             first = next(objects)
  221.         except TypeError:
  222.             first = next(iter(objects))
  223.  
  224.         value = get_marshalable(first)
  225.         yield insert_with_valuesv(-1, columns, [
  226.             value])
  227.         if _gets_marshaled_to_pyobject(first):
  228.             for obj in objects:
  229.                 yield insert_with_valuesv(-1, columns, [
  230.                     obj])
  231.             
  232.         else:
  233.             for obj in objects:
  234.                 value = get_marshalable(obj)
  235.                 yield insert_with_valuesv(-1, columns, [
  236.                     value])
  237.             
  238.  
  239.     
  240.     def append_many(self, objects):
  241.         '''Append a list of python objects'''
  242.         for i in self.iter_append_many(objects):
  243.             pass
  244.         
  245.  
  246.     
  247.     def insert_many(self, position, objects):
  248.         if position == -1 or position > len(self):
  249.             self.append_many(objects)
  250.             return None
  251.         insert_with_valuesv = None.insert_with_valuesv
  252.         get_marshalable = self._get_marshalable
  253.         columns = [
  254.             0]
  255.         for i, obj in enumerate(objects):
  256.             value = get_marshalable(obj)
  257.             insert_with_valuesv(position + i, columns, [
  258.                 value])
  259.         
  260.  
  261.     
  262.     def insert_before(self, sibling, row = None):
  263.         if row is not None:
  264.             value = self._get_marshalable(row[0])
  265.             if sibling is None:
  266.                 position = -1
  267.             else:
  268.                 position = self.get_path(sibling)[0]
  269.             return self.insert_with_valuesv(position, [
  270.                 0], [
  271.                 value])
  272.         if not not (None.ATOMIC):
  273.             raise AssertionError
  274.         return None(ObjectStore, self).insert_before(sibling)
  275.  
  276.     
  277.     def insert_after(self, sibling, row = None):
  278.         if row is not None:
  279.             value = self._get_marshalable(row[0])
  280.             if sibling is None:
  281.                 position = 0
  282.             else:
  283.                 position = self.get_path(sibling)[0] + 1
  284.             return self.insert_with_valuesv(position, [
  285.                 0], [
  286.                 value])
  287.         if not not (None.ATOMIC):
  288.             raise AssertionError
  289.         return None(ObjectStore, self).insert_after(sibling, row)
  290.  
  291.     
  292.     def prepend(self, row = None):
  293.         return self.insert(0, row)
  294.  
  295.  
  296.